package com.tanhua.sso.controller;

import com.tanhua.sso.service.SmsService;
import com.tanhua.sso.vo.ErrorResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @program: my-tanhua
 * @description:
 * @author: xuejingjing
 * @create: 2021-09-12 16:35
 **/

@RestController
@RequestMapping("user")
@Slf4j
public class SmsController {

    @Autowired
    private SmsService smsService;
    /**
     * 发送短信验证码接口
     * @param param
     * @return
     */
    @PostMapping("login")
    //需要传一个手机号，但是这个手机号是json类型;获得请求参数
    //为什么要用Map呢？因为前端的格式是{"phone","conselectetur eu"}
    public ResponseEntity<ErrorResult>  sendCheckCode(@RequestBody Map<String,String> param) {

        ErrorResult errorResult= null;
        String phone = param.get("phone");
        try{
        //先发送短信验证码，在校验短信验证码。
        errorResult = smsService.sendCheckCode(phone);


        if (null == errorResult) {
            return ResponseEntity.ok(null);
        }
    }catch(Exception e){
        log.error("发送短信验证码失败~"+phone,e);
    }
    return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }

}